/*
 * @Author: wangming
 * @Date: 2021-03-30 17:37:25
 * @LastEditors: wangming
 * @LastEditTime: 2022-07-21 17:14:58
 * @Description: file content
 */
import { cloneDeep } from 'lodash';
import router from '@/router'
import EventBus from '@/utils/eventBus'
/**
 * tag 是否复用tag的逻辑验证
 * @param {*} newview 
 * @param {*} oldview 
 * @returns 
 */
function checkTags(newview, oldview, title) {
  let { pageKey, processKey } = newview.query
  return oldview.path === newview.path && pageKey === oldview.query.pageKey && (title === oldview.meta.title || processKey === oldview.query.processKey)
}

const state = {
  keepAlive: false,
  visitedViews: [],
  fullPath: '',
  deletePath: [],//历史删除过的tag
};
const mutations = {
  SET_KEEP_ALIVE: (state, keepAlive) => {
    state.keepAlive = keepAlive
  },
  SET_FULLPATH: (state, fullPath) => {
    state.fullPath = fullPath
  },
  ADD_VISITED_VIEW: (state, view) => {
    var title = decodeURIComponent(view.query.title || view.meta.title)
    if (state.visitedViews.some((v) => checkTags(view, v, title))) {
      state.visitedViews.forEach((v) => {
        if (checkTags(view, v, title)) {
          let { query, params, fullPath } = view;
          fullPath !== v.fullPath && state.deletePath.push(v.fullPath)
          Object.assign(v, { query, params, fullPath });
        }
      });
    } else {
      Object.assign(view.meta, { title })
      state.visitedViews.push(view);
    }
    document.title = view.appName + '-' + title
  },
  DEL_VISITED_VIEW: (state, index) => {
    let { fullPath } = state.visitedViews[index] || {}
    if (!fullPath) return
    state.deletePath.push(fullPath)
    state.visitedViews.splice(index, 1);
    EventBus.$emit('deleteTag', fullPath)
  },
  CLEAR_VISITED_VIEW: (state) => {
    state.visitedViews = [];
  },
  SET_VISITED_VIEW_TITLE: (state, { route, title, appName }) => {
    state.visitedViews.forEach((i) => {
      if (checkTags(i, route, title)) {
        i.meta.title = title;
        document.title = appName + '-' + title
      }
    });
  },
  SET_VISITED_VIEW_DATA: (state, { key, data }) => {
    const { fullPath } = router.currentRoute
    state.visitedViews.forEach((i) => {
      if (i.fullPath === fullPath) {
        i.tableData[key] = cloneDeep(data)
      }
    });
  }
};
const actions = {
  async setRouteAlive({ commit }, fullPath) {
    let index = state.deletePath.indexOf(fullPath)
    let keepAlive = index === -1
    index > -1 && state.deletePath.splice(index, 1)
    commit('SET_KEEP_ALIVE', keepAlive)
  },
  setKeepAlive({ commit }, keepAlive) {
    commit('SET_KEEP_ALIVE', keepAlive)
  },
  changeTabCatch({ commit }, view) {
    commit('SET_VISITED_VIEW_DATA', view)
  },
  addVisitedView({ commit, getters }, view) {
    let { fullPath, path, name, meta, query } = view;
    commit('ADD_VISITED_VIEW', {
      appName: getters.APP_INFO.appName,
      tableData: {},
      fullPath,
      path,
      name,
      meta: cloneDeep(meta),
      query: cloneDeep(query),
    });
  },
  delVisitedView({ commit }, index) {
    commit('DEL_VISITED_VIEW', index);
  },
  delVisitedViewByPath({ commit }, path) {
    let index = state.visitedViews.findIndex(v => v.path === path)
    commit('DEL_VISITED_VIEW', index);
  },
  setViewTitle({ commit, getters }, tag) {
    tag.appName = getters.APP_INFO.appName
    commit('SET_VISITED_VIEW_TITLE', tag);
  },

};
const getters = {
  getAlive({ keepAlive }) {
    return keepAlive
  },
  getVisitedViews({ visitedViews }) {
    return visitedViews;
  },
  getViewsConfig({ visitedViews, fullPath }) {
    let config = visitedViews.find(i => i.fullPath === fullPath) || {}
    return config.tableData || {}
  }
};

export default {
  getters,
  state,
  mutations,
  actions,
};


